﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using PeoplesJukebox.Web.Enums;
using PeoplesJukebox.Web.Pages;

namespace PeoplesJukebox.Web
{
    /// <summary>
    /// Main application page, handles navigation between pages
    /// </summary>
    public partial class Page : UserControl
    {
        private List<PageBase> _pages;
        private PageBase PreviousPage { get; set; }
        private PageBase CurrentPage { get; set; }

        private List<PageBase> Pages
        {
            get
            {
                if (_pages == null)
                {
                    _pages = new List<PageBase>();
                    _pages.Add(this.ucHomePage);
                    _pages.Add(this.ucListPage);
                    _pages.Add(this.ucSearchPage);
                    _pages.Add(this.ucSelectPage);
                    _pages.Add(this.ucStartPage);
                    _pages.Add(this.ucSuggestPage);
                    _pages.Add(this.ucConfirmTrackSelection);
                }
                return _pages;
            }
        }

        /// <summary>
        /// Default constructor
        /// </summary>
        public Page()
        {
            InitializeComponent();
            foreach (PageBase page in Pages)
            {
                page.Complete += new PageBase.OnCompleteEventHandler(PageComplete);
                page.Cancelled += new PageBase.OnCancelledEventHandler(PageCancelled);
            }
            this.CurrentPage = this.ucStartPage;
        }

        private void PageCancelled()
        {
            this.CurrentPage.Visibility = Visibility.Collapsed;
            this.PreviousPage.Visibility = Visibility.Visible;
            this.CurrentPage = this.PreviousPage;
        }

        private void PageComplete(PageType nextPageType)
        {
            this.btnHome.Visibility = (nextPageType == PageType.Home || nextPageType == PageType.Start) ? Visibility.Collapsed : Visibility.Visible;
            if (this.CurrentPage == this.ucHomePage)
            {
                this.PreviousPage = null;

            }
            else
            {
                this.PreviousPage = this.CurrentPage;
            }
            //show the next page in the workflow:
            this.CurrentPage = this.GetNextPage(nextPageType);
            //unless it's the confirmation page, hide the previous pages:
            if (this.CurrentPage.PageType != PageType.ConfirmTrackSelection)
            {
                this.Pages.ForEach((PageBase page) =>
                {
                    page.Visibility = Visibility.Collapsed;
                });
            }
            //copy any data context:
            if (this.PreviousPage != null)
            {
                this.CurrentPage.DataContext = this.PreviousPage.DataContext;
            }
            this.CurrentPage.Visibility = Visibility.Visible;
            this.CurrentPage.Reset();
        }

        private PageBase GetCurrentPage()
        {
            PageBase currentPage = null;
            foreach (PageBase page in this.Pages)
            {
                if (page.Visibility == Visibility.Visible)
                {
                    currentPage = page;
                    break;
                }
            }
            return currentPage;
        }

        private PageBase GetNextPage(PageType pageType)
        {
            PageBase nextPage = null;
            foreach (PageBase page in Pages)
            {
                if (page.PageType == pageType)
                {
                    nextPage = page;
                    break;
                }
            }
            return nextPage;
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            this.PageComplete(PageType.Home);
        }
    }
}